package com.fzu.orderservice.config;

import com.fzu.commonutil.mapper.OrderFormMapper;
import com.fzu.commonutil.util.ApiException;
import com.fzu.commonutil.util.ErrorEnum;
import com.fzu.commonutil.util.RedisKey;
import com.fzu.commonutil.util.RedisUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 初始化任务
 * 订单数初始化
 */
@Component
@Order(value = 1)
@RequiredArgsConstructor
@Slf4j
public class StartupRunner implements CommandLineRunner {
    private final OrderFormMapper orderFormMapper;
    private final RedisUtil redisUtil;

    @Override
    public void run(String... args) throws Exception {
        boolean result = redisUtil.set(RedisKey.ORDER_COUNT, orderFormMapper.selectCount(null));
        if (result) {
            log.info("【订单总数初始化成功！】");
        } else {
            log.error("【订单总数初始化失败！】");
            throw new ApiException(ErrorEnum.INIT_ERROR);
        }
    }
}
